package com.adtech.adms.mainview.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.adtech.adms.SessionKey;
import com.adtech.adms.commons.ErrorCode;
import com.adtech.adms.commons.web.intercepter.AuthInterceptor;
import com.adtech.adms.commons.web.listener.OnlineUserListener;
import com.adtech.adms.system.LogConstants;
import com.adtech.adms.system.pojo.User;
import com.adtech.adms.system.service.FeatureService;
import com.adtech.adms.system.service.UserService;
import com.adtech.core.exception.ServiceException;
import com.adtech.core.web.action.BaseAction;

/**
 * 登录action
 * 实现用户登录功能
 * @author Dormin 
 * @since 2011-6-7 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public class LoginAction extends BaseAction implements ServletRequestAware {

	private static final long serialVersionUID = 1L;
	
	private static final String TEXT_FORCE_OFFLINE = "session.force.offline";

	private UserService userService;
	private FeatureService featureService;
//	private SystemLogService systemLogService;
	private String userName;
	private String password;
	private String goingToURL;

	public FeatureService getFeatureService() {
		return featureService;
	}

	public void setFeatureService(FeatureService featureService) {
		this.featureService = featureService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

//	public SystemLogService getSystemLogService() {
//		return systemLogService;
//	}
//
//	public void setSystemLogService(SystemLogService systemLogService) {
//		this.systemLogService = systemLogService;
//	}

	/**
	 * 列表初始页面的转发
	 */
	@Override
	public String execute() {
		String ip = AuthInterceptor.getIpAddr(httpRequest);
		try {
			long start = System.currentTimeMillis();
			Object obj = session.get(SessionKey.SESSION_KEY_USER);
			if (obj != null && obj instanceof User) {
				HttpSession oldSession = httpRequest.getSession(false);
				if(oldSession!=null) {
					oldSession.invalidate();
				}
				httpRequest.getSession(true);
				session = new SessionMap<String, Object>(httpRequest);
			}
			User user = userService.login(userName!=null?userName.toLowerCase():null, password,ip,httpRequest.getSession().getId(),getText(TEXT_FORCE_OFFLINE));
			session.put(SessionKey.SESSION_KEY_USER, user);
			OnlineUserListener.successLogin(user.getLoginIp());
			return SUCCESS;
		} 
		catch (ServiceException e) {
			User user = new User();
			user.setLoginName("系统");
			user.setLoginIp(ip);
			if(ErrorCode.USER_NOT_EXIST == e.getErrorCode()) {
				if(userName!=null) {
//					systemLogService.addLog(LogConstants.LOG_TYPE_ILLLEGAL_USER,new Object[]{userName},user);
				}
			} else if(ErrorCode.USER_PWD_NOT_MATCH == e.getErrorCode()) {
//				systemLogService.addLog(LogConstants.LOG_TYPE_ERROR_PASSWORD,new Object[]{userName},user);
			}
			addActionError(e.getErrorMessage());
			OnlineUserListener.failLogin(ip);
			return LOGIN;
		}

	}
	


	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getGoingToURL() {
		return goingToURL;
	}

	public void setGoingToURL(String goingToURL) {
		this.goingToURL = goingToURL;
	}


	private HttpServletRequest httpRequest;

	public void setServletRequest(HttpServletRequest request) {
		this.httpRequest = request;

	}

}
